home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cmprss.exe / DECPRESS.CPP < prev    next >
C/C++ Source or Header  |  1993-01-26  |  2KB  |  169 lines

  1. #include "decpress.cls"
  2.  
  3. cpifstream::cpifstream(void) : idx(0), o_len(0), all_done(0), count(0)
  4.     {
  5.     }
  6.  
  7.  
  8. cpifstream::cpifstream(char *fn) : idx(0), o_len(0), all_done(0), count(0)
  9.     {
  10.     init();
  11.     ifstream::open(fn, ios::binary | ios::in);
  12.     flags(0);            // clear all flags (especially the skip white space flag)
  13.     }
  14.  
  15.  
  16. cpifstream::~cpifstream()
  17.     {
  18.     clear();
  19.     }
  20.  
  21.  
  22. cpifstream& cpifstream::operator>>(char* s)
  23.     {
  24. char c;
  25.  
  26.     do
  27.         {
  28.         if (idx==o_len)
  29.             decode();
  30.         *s=c=outbuf[idx++];
  31.         s++;
  32.         count++;
  33.         } while (c);
  34.     return *this;
  35.     }
  36.  
  37.  
  38. cpifstream& cpifstream::ignore(int n, int delim)
  39.     {
  40.     if (!n)
  41.         return *this;
  42.  
  43.      char c;
  44.     do
  45.         {
  46.         c=get();
  47.         } while ( (c!=delim) && (--n!=0) && (!all_done) );
  48.     return *this;
  49.     }
  50.  
  51.  
  52. int cpifstream::get(void)
  53.     {
  54.     if (all_done)
  55.         return EOF;
  56.  
  57.     unsigned char c;
  58.     getbuf(&c,sizeof(unsigned char));
  59.     count++;
  60.     return c;
  61.     }
  62.  
  63.  
  64. int cpifstream::peek(void)
  65.     {
  66.     if (all_done)
  67.         return EOF;
  68.  
  69.     if (idx==o_len)
  70.         decode();
  71.     return outbuf[idx];
  72.     }
  73.  
  74.  
  75. void cpifstream::open(char *fn)
  76.     {
  77.     init();
  78.     ifstream::open(fn, ios::binary | ios::in);
  79.     flags(0);            // clear all flags (especially the skip white space flag)
  80.     all_done=0;
  81.     }
  82.  
  83.  
  84. filebuf* cpifstream::close(void)
  85.     {
  86.     clear();
  87.     return rdbuf()->close();
  88.     }
  89.  
  90.  
  91. void cpifstream::init(void)
  92.     {
  93.     inbuf=new unsigned char[CBUF_SIZE];
  94.     outbuf=new unsigned char[CBUF_SIZE];
  95.     }
  96.  
  97.  
  98. void cpifstream::clear(void)
  99.     {
  100.     delete inbuf;
  101.     delete outbuf;
  102.     }
  103.  
  104.  
  105. void cpifstream::decode(void)
  106.     {
  107.     idx=0;
  108.  
  109.     istream::read((char*)&o_len,2);
  110.     if (o_len==0)
  111.         {
  112.         all_done=1;
  113.         return;
  114.         }
  115.  
  116.     if (o_len<0)
  117.         {
  118.         o_len=0-o_len;
  119.         istream::read(outbuf,o_len);
  120.         }
  121.     else
  122.         {
  123.         istream::read(inbuf,o_len);
  124.         o_len=decompress(inbuf,o_len,outbuf);
  125.         }
  126.     }
  127.  
  128.  
  129. void cpifstream::getbuf(void* v, int n)
  130.     {
  131.     while (n--)
  132.         {
  133.         if (idx==o_len)
  134.             decode();
  135.         *(char*)v=outbuf[idx++];
  136.         ((char*)v)++;
  137.         count++;
  138.         }
  139.     }
  140.  
  141.  
  142. void cpifstream::getbuf2(char* s, int l, char delim, char ed)
  143.     {
  144.     if (all_done)
  145.         return;
  146.     if (l<2)            // read l-1 bytes, so l must be 2 or more
  147.         {
  148.         *s='\0';
  149.         return;
  150.         }
  151.  
  152.     char c;
  153.     do
  154.         {
  155.         if (!ed)                        // if we aren't extracting the delimeter
  156.             {
  157.             if (peek()==delim)                // don't extract delimiter
  158.                 {
  159.                 s++;                        // necessary for s-- outside of loop
  160.                 break;
  161.                 }
  162.             }
  163.         *s++=c=get();
  164.         } while ( (c!=delim) && (--l!=0) && (!all_done) );
  165.     s--;
  166.     *s='\0';
  167.     }
  168.  
  169.